﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.Globalization;
using System.ComponentModel;

namespace Beetle.Web.UI.WebControls
{
    /// <summary>
    /// GridView LinkButton模版列
    /// </summary>
    /// <!-- 作者:韦腾  时间:2010-04-09 -->
    public class LinkButtonField : System.Web.UI.WebControls.ButtonField
    {
        private const string ThisExpression = "!";
        private PropertyDescriptor _boundFieldDesc = null;

        public LinkButtonField()
        {
            this.ButtonType = ButtonType.Link;
        }

        public string DefaultAlertMessage
        {
            get
            {
                object o = ViewState["DefaultAlertMessage"];
                return o != null ? (string)o : string.Empty;
            }

            set
            {
                ViewState["DefaultAlertMessage"] = value;
            }
        }


        public string DataField
        {
            get
            {
                object o = ViewState["DataField"];
                return o != null ? (string)o : string.Empty;
            }

            set
            {
                ViewState["DataField"] = value;
            }
        }


        public string DataFormatString
        {
            get
            {
                object o = ViewState["DataFormatString"];
                return o != null ? (string)o : string.Empty;
            }

            set
            {
                ViewState["DataFormatString"] = value;
            }
        }


        public bool HtmlEncode
        {
            get
            {
                object o = ViewState["HtmlEncode"];
                return o != null ? (bool)o : false;
            }

            set
            {
                ViewState["HtmlEncode"] = value;
            }
        }


        protected virtual string FormatDataValue(object dataValue, bool encode)
        {
            string str = string.Empty;
            if (null == dataValue)
            {
                return str;
            }

            if (this.DataFormatString.Length == 0)
            {
                return dataValue.ToString();
            }

            str = string.Format(CultureInfo.CurrentCulture, this.DataFormatString, new object[] { dataValue });

            if (encode)
            {
                str = HttpUtility.HtmlEncode(str);
            }

            return str;
        }

        public override void InitializeCell(System.Web.UI.WebControls.DataControlFieldCell cell, System.Web.UI.WebControls.DataControlCellType cellType, System.Web.UI.WebControls.DataControlRowState rowState, int rowIndex)
        {
            base.InitializeCell(cell, cellType, rowState, rowIndex);

            LinkButton lknButton = null;
            foreach (Control item in cell.Controls)
            {
                if (item is LinkButton)
                {
                    lknButton = item as LinkButton;
                }
            }

            if (lknButton != null)
            {
                if (this.DataField.Length != 0)
                {
                    lknButton.DataBinding += new EventHandler(this.OnDataBindField);
                }
                else
                {
                    this.ApplyLinkButtonClientClick(lknButton, this.DefaultAlertMessage);
                }
            }
        }

        private void ApplyLinkButtonClientClick(LinkButton lknButton, string msg)
        {
            lknButton.OnClientClick = "if(!window.confirm('" + msg + "')){return false;}";
        }

        protected virtual object GetValue(Control controlContainer)
        {
            object component = null;
            string dataField = this.DataField;

            component = DataBinder.GetDataItem(controlContainer);
            if ((component == null) && !base.DesignMode)
            {
                throw new HttpException("无法找到数据容器。");
            }

            if ((this._boundFieldDesc == null) && !dataField.Equals(ThisExpression))
            {
                this._boundFieldDesc = TypeDescriptor.GetProperties(component).Find(dataField, true);
                if ((this._boundFieldDesc == null) && !base.DesignMode)
                {
                    throw new HttpException(string.Format("无法找到字段：{0}", new object[] { dataField }));
                }
            }

            if ((this._boundFieldDesc != null) && (component != null))
            {
                return this._boundFieldDesc.GetValue(component);
            }

            if (base.DesignMode)
            {
                return this.HeaderText;
            }

            return component;
        }

        private void OnDataBindField(object sender, EventArgs e)
        {
            LinkButton control = (LinkButton)sender;
            Control namingContainer = control.NamingContainer;
            object dataValue = this.GetValue(namingContainer);

            string msg = this.FormatDataValue(dataValue, this.HtmlEncode);

            if (string.IsNullOrEmpty(msg))
            {
                msg = this.DefaultAlertMessage;
            }

            this.ApplyLinkButtonClientClick(control, msg);
        }
    }
}
